package com.byxy.managerial.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.byxy.managerial.entity.Department;
import com.byxy.managerial.entity.Staff;
import com.byxy.managerial.service.DepartmentService;
import com.byxy.managerial.service.StaffService;
import com.opensymphony.xwork2.ActionSupport;

public class StaffAddAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private List<Department> list;
	@Resource
	private DepartmentService departmentService;

	// 动态绑定部门到select标签
	public String getDepart() {
		list = departmentService.find();// 获取所有部门
		return SUCCESS;
	}

	@Resource
	private StaffService staffService;
	private String sid;// 员工编号
	private String sname; // 姓名
	private Character ssex;// 性别
	private Integer sage;// 年龄
	private Date joindate; // 入职时间
	private Integer departid;// 所属部门id

	// 添加员工的方法
	public void addStaff() throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		Map<String, Object> map = new HashMap<String, Object>();
		Department depart = new Department();
		depart.setId(departid);
		Staff staff = new Staff(sid, sname, ssex, sage, joindate, depart);
		boolean isAdd = staffService.save(staff);
		if (isAdd) {
			map.put("isAdd", isAdd);
		}else {
			map.put("isAdd", "该员工编号已存在！");
		}
		PrintWriter pw = response.getWriter();
		String str = JSON.toJSONString(map);
		pw.write(str);
		pw.flush();
		pw.close();
	}

	public List<Department> getList() {
		return list;
	}

	public void setList(List<Department> list) {
		this.list = list;
	}

	public String getSid() {
		return sid;
	}

	public void setSid(String sid) {
		this.sid = sid;
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public Character getSsex() {
		return ssex;
	}

	public void setSsex(Character ssex) {
		this.ssex = ssex;
	}

	public Integer getSage() {
		return sage;
	}

	public void setSage(Integer sage) {
		this.sage = sage;
	}

	public Date getJoindate() {
		return joindate;
	}

	public void setJoindate(Date joindate) {
		this.joindate = joindate;
	}

	public Integer getDepartid() {
		return departid;
	}

	public void setDepartid(Integer departid) {
		this.departid = departid;
	}

}
